﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using mighty.on;
using System.Net;


namespace Mighty.Mighty
{
    public partial class btnReload : Form
    {
        GameService gameService = new GameService();
        Service webService = new Service();
        OnlinePlayer myInfo = new OnlinePlayer();
        List<ClickArea> clickAreaList = new List<ClickArea>();
        List<Room> roomList = new List<Room>();
        List<OnlinePlayer> playerList = new List<OnlinePlayer>();

        int scrollRoom;
//        int scrollPlayer;

        public btnReload()
        {
            InitializeComponent();
            
        }

        public btnReload(String id)
        {
            InitializeComponent();
            myInfo.id = id;

            IPHostEntry ipHostEntry = Dns.GetHostEntry(Dns.GetHostName());
            myInfo.ip = String.Format("{0}",ipHostEntry.AddressList[0]);
            myInfo.port = 7007;

            webService.JoinRoom(-1, myInfo.ip, myInfo.port, myInfo.id);
            initClickAreaList();

        }

        public void initClickAreaList()
        {
            clickAreaList.Add(new ClickArea(AreaType.ScollUp, new Rectangle(512, 133, 20, 20)));
            clickAreaList.Add(new ClickArea(AreaType.ScollDown, new Rectangle(512, 368, 20, 20)));
            clickAreaList.Add(new ClickArea(AreaType.RoomList, new Rectangle(100, 140, 200, 245)));
            clickAreaList.Add(new ClickArea(AreaType.NickList, new Rectangle(600, 140, 80, 245)));
        }


        private void LobbyForm_Paint(object sender, PaintEventArgs e)
        {
            Graphics drawingSurface = e.Graphics;
           // Rectangle realClientArea = ClientRectangle;
 
            DrawRoomList(drawingSurface);
            DrawPlayerList(drawingSurface);
            DrawMyInfo(drawingSurface);
            
        }

        private void DrawMyInfo(Graphics drawingSurface)
        {
            StringFormat myFormat = new StringFormat();
            drawingSurface.DrawString(String.Format("{0}", myInfo.id), new Font("굴림", 9), Brushes.Black, new Point(150, 450), myFormat);
            drawingSurface.DrawString(String.Format("{0}", myInfo.ip), new Font("굴림", 9), Brushes.Black, new Point(150, 470), myFormat);
        }



        private void DrawRoomList(Graphics drawingSurface)
        {
            try
            {
                gameService = webService.ShowRoomList();
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            } 

            StringFormat myFormat = new StringFormat();
            Room tmpRoom = new Room();

            int tmpY = 145;
            int tmpRow = 0;
            int tmpScroll = scrollRoom;

            for (int i = 0; i < 20; i++)
            {
                if (i + scrollRoom > 19) break;

                tmpRoom = gameService.roomList[i + scrollRoom];
               
                if (tmpRoom.title != "")
                {
                    if (tmpRow == 9) break;

                    myFormat.Alignment = StringAlignment.Center;
                    drawingSurface.DrawString(String.Format("{0}",tmpRoom.no), new Font("굴림", 9), Brushes.Black, new Point(50, tmpY), myFormat);
                    myFormat.Alignment = StringAlignment.Near;
                    drawingSurface.DrawString(tmpRoom.title, new Font("굴림", 9), Brushes.Black, new Point(100, tmpY), myFormat);
                    myFormat.Alignment = StringAlignment.Center;
                    drawingSurface.DrawString(tmpRoom.owner.id, new Font("굴림", 9), Brushes.Black, new Point(330, tmpY), myFormat);
                    drawingSurface.DrawString(Convert.ToString(tmpRoom.seed), new Font("굴림", 9), Brushes.Black, new Point(415, tmpY), myFormat);
                    tmpY += 28;

                    tmpRow++;
                    
                }
            }
           
        }

        private void DrawPlayerList(Graphics drawingSurface)
        {
            gameService.playerList = webService.ShowPlayerList();
            StringFormat myFormat = new StringFormat();
            Room tmpRoom = new Room();
            String strRoom;

            int tmpY = 145;
            int tmpRow = 0;
            
            foreach(OnlinePlayer op in gameService.playerList)
            {
                if (tmpRow == 9) break;
                myFormat.Alignment = StringAlignment.Near;
                drawingSurface.DrawString(String.Format("{0}", op.id), new Font("굴림", 9), Brushes.Black, new Point(600, tmpY), myFormat);


                if (op.roomNo == -1) strRoom = "로비";
                else strRoom = String.Format("{0}번방", op.roomNo);

                myFormat.Alignment = StringAlignment.Center;
                drawingSurface.DrawString(strRoom, new Font("굴림", 9), Brushes.Black, new Point(710, tmpY), myFormat);
                  
                tmpY += 28;
                tmpRow++;
                
            }
        }

        private void EnterRoom(int roomNo)
        {
            Room room = new Room();

            room = gameService.roomList[roomNo];
            MessageBox.Show(Convert.ToString(room.no));

            Mighty m = new Mighty(myInfo, room.no);
            
            
            webService.JoinRoom(room.no, myInfo.ip, myInfo.port, myInfo.id);

            m.Show();
            this.Visible = false;

        }

        private void LobbyForm_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            int tmpY = 0;
            int tmpMod = 0;

            // 방 리스트 
            if (e.X > 100 && e.X < 300)
            {
                if (e.Y > 140)
                {
                    tmpY = e.Y - 140;
                    tmpMod = tmpY % 28;
                    if (tmpMod < 20)
                        EnterRoom(tmpY / 28 + scrollRoom);

                }
            }

        }



        private void LobbyForm_MouseMove(object sender, MouseEventArgs e)
        {

                int y = e.Y - 140;
                String str = String.Format("X좌표:{3} /Y좌표:{0} /mod :{1}/ tmpY:{2}",e.Y, y % 28, y / 28,e.X);

                txtChatView.Text = str;
            
        }

        private void LobbyForm_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Y > 133 && e.Y < 153 && e.X > 512 && e.X < 532)
            {
                if (scrollRoom > 0) scrollRoom--;
                // MessageBox.Show(Convert.ToString(scrollRoom));
                Invalidate();
            }
            else if (e.Y > 368 && e.Y < 388 && e.X > 512 && e.X < 532)
            {
                if (scrollRoom < 11) scrollRoom++;
                // MessageBox.Show(Convert.ToString(scrollRoom));
                Invalidate();
            }
        }

        private void LobbyForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        private void btnReload_Click(object sender, EventArgs e)
        {
            Invalidate();
        }

        private void btnMakeRoom_Click(object sender, EventArgs e)
        {
            CreateRoomForm crform = new CreateRoomForm(myInfo);
            crform.ShowDialog();

        }

 
    }

    public enum AreaType
    {
        ScollUp,
        ScollDown,
        RoomList,
        NickList
    }

    public class ClickArea
    {
        public Rectangle area;
        public AreaType type;

        public ClickArea(AreaType t, Rectangle a)
        {
            type = t;
            area = a;
        }



    }
}
